import { createStore } from 'vuex'
import axios from 'axios'

const store = createStore({
    state(){
        return{
            header:{},
            goods:[],
            carList:[],
            price:0,
            newArr:[]
        }
    },
    getters:{

    },
    mutations:{
        setHeader(state, payload) {
            state.header = payload.data;
          },
        setGoods(state,payload){
            state.goods = payload.data
        },
        setCarList(state, payload) {
            const hasItem = state.carList.findIndex((item) => {
              return item.name == payload.item.name;
            });
      
            if (hasItem == "-1") {
              state.carList.push(payload.item);
            }
            console.log(hasItem)
        },

        
    },
    actions:{
        async getHeader({ commit }) {
            const {data:{data}} = await axios.get("/api/header");
            // console.log(data)
            commit("setHeader",{
                data
            })
          },
          async getGoods({ commit }) {
            const {
              data: { data },
            } = await axios.get("/api/goods");
            // console.log(data)
            commit("setGoods", { data });
          },
    },
}) 

export default store